package com.SpringAOP.test_1_31_springaop.cglbProxy;

import com.SpringAOP.test_1_31_springaop.dynamicProxy.JDKInvocation;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class Main {
    public static void main(String[] args) {
        // 目标对象
       HouseSubject subject = new RealHouseSubject();

       // 目标对象2 代理类
        HouseSubjectClass subjectClass = new HouseSubjectClass();
       // CGLib 动态代理

        // /**
        // * 参数说明:
        // * o: 被代理的对象
        // * method: ⽬标⽅法(被拦截的⽅法, 也就是需要增强的⽅法)
        // * objects: ⽅法⼊参
        // * methodProxy: ⽤于调⽤原始⽅法
        // */
        // Object intercept(Object o, Method method, Object[] objects, MethodProxy
        //  methodProxy) throws Throwable;
        HouseSubject proxy =  (HouseSubject) Enhancer.create(subject.getClass(),new CGLibIntercepter(subject)); // 这也是一个固定的写法
                                            // 表示要代理的目标对象是谁
        proxy.rentHouse();
        proxy.saleHouse();

        // CGlib 代理类
        HouseSubjectClass proxy1 = (HouseSubjectClass) Enhancer.create(subjectClass.getClass(), new CGLibIntercepter(subjectClass));
        proxy1.rentHouse();
    }
}
